# coding=utf-8
import codecs
import os
import unittest

Zemberek_Resources_Folder = "../../zemberek/src/main/resources/tr/"
Trnltk_Resources_Folder = "../core/src/main/resources/"


def word_ends_with_one_of(line, endings):
    word = ""
    if '[' in line:
        word = line.split('[')[0].strip()
    else:
        word = line
    return any([word.endswith(e) for e in endings])


ends_with_giller = lambda line: word_ends_with_one_of(line, [u"giller"])
ends_with_lerLar = lambda line: word_ends_with_one_of(line, [u"ler", u"lar"])
ends_with_enAn = lambda line: word_ends_with_one_of(line, [u"en", u"an"])
ends_with_lanmakLenmek = lambda line: word_ends_with_one_of(line, [u"lenmek", u"lanmak"])
ends_with_siSu = lambda line: word_ends_with_one_of(line, [u"si", u"su", u"sı", u"sü"])
ends_with_likLuk = lambda line: word_ends_with_one_of(line, [u"lik", u"luk", u"lık", u"lük"])
ends_with_caCe = lambda line: word_ends_with_one_of(line, [u"ca", u"ce", u"ça", u"çe"])
ends_with_casinaCesine = lambda line: word_ends_with_one_of(line, [u"casına", u"cesine", u"çasına", u"çesine"])
ends_with_dirmakDirmek = lambda line: word_ends_with_one_of(line, [u"dırmak", u"dirmek", u"tırmak", u"tirmek", u"durmak", u"dürmek", u"turmak", u"türmek"])
ends_with_tmakTmek = lambda line: word_ends_with_one_of(line, [u"tmak", u"tmek"])
ends_with_rmakRmek = lambda line: word_ends_with_one_of(line, [u"rmak", u"rmek"])
ends_with_ilmakIlmek = lambda line: word_ends_with_one_of(line, [u"ılmak", u"ilmek", u"ulmak", u"ülmek"])
ends_with_mazMez = lambda line: word_ends_with_one_of(line, [u"maz", u"mez"])
ends_with_misMus = lambda line: word_ends_with_one_of(line, [u"mış", u"miş", u"muş", u"müş"])
ends_with_ciCu = lambda line: word_ends_with_one_of(line, [u"cı", u"ci", u"cu", u"cü", u"çı", u"çi", u"çu", u"çü"])


class DictionaryOps(unittest.TestCase):
    def test_should_print_duplicate_lines_in_zemberek_master_and_secondary_dictionary(self):
        master_dict_lines = None
        secondary_dict_lines = None

        with codecs.open(self._get_zemberek_resource("master-dictionary.dict"), encoding="utf-8") as master_dict:
            master_dict_lines = set([line for line in master_dict])

        with codecs.open(self._get_zemberek_resource("secondary-dictionary.dict"), encoding="utf-8") as secondary_dict:
            secondary_dict_lines = set([line for line in secondary_dict])

        intersection = list(master_dict_lines.intersection(secondary_dict_lines))

        intersection = sorted(intersection)

        for line in intersection:
            print line.strip()

    def test_should_print_duplicate_lines_in_trnltk_master_dict_and_zemberek_secondary_dictionary(self):
        master_dict_lines = None
        secondary_dict_lines = None

        with codecs.open(self._get_trnltk_resource("master-dictionary.dict"), encoding="utf-8") as master_dict:
            master_dict_lines = set([line for line in master_dict])

        with codecs.open(self._get_zemberek_resource("secondary-dictionary.dict"), encoding="utf-8") as secondary_dict:
            secondary_dict_lines = set([line for line in secondary_dict])

        intersection = list(master_dict_lines.intersection(secondary_dict_lines))

        intersection = sorted(intersection)

        for line in intersection:
            print line.strip()

    @unittest.skip("skipped")
    def test_should_remove_lines_from_zemberek_master_dictionary(self):
        lines_to_remove = u"""
        çıkartmak
        ürkütmek
        """.strip().splitlines(False)

        lines_to_remove = [l.strip() for l in lines_to_remove]

        def in_lines(line):
            return line in lines_to_remove

        self._process_zemberek_master_dictionary(in_lines)

    @unittest.skip("skipped")
    def test_should_remove_lines_from_trnltk_master_dictionary(self):
        lines_to_remove = u"""
        yöneltmek
        yürütmek
        """.strip().splitlines(False)

        lines_to_remove = [l.strip() for l in lines_to_remove]

        def in_lines(line):
            return line in lines_to_remove

        self._process_trnltk_master_dictionary(in_lines)

    @unittest.skip("skipped")
    def test_should_remove_proper_nouns_from_zemberek_master_dictionary(self):
        self._process_zemberek_master_dictionary(lambda line: line[0].isupper())

    @unittest.skip("skipped")
    def test_should_remove_words_with_giller_from_zemberek_master_dictionary(self):
        self._process_zemberek_master_dictionary(ends_with_giller)

    @unittest.skip("skipped")
    def test_should_remove_words_with_giller_from_trnltk_master_dictionary(self):
        self._process_trnltk_master_dictionary(ends_with_giller)

    @unittest.skip("skipped")
    def test_should_remove_words_with_lerLar_from_zemberek_master_dictionary(self):
        self._process_zemberek_master_dictionary(ends_with_lerLar)

    @unittest.skip("skipped")
    def test_should_remove_words_with_lerLar_from_trnltk_master_dictionary(self):
        self._process_trnltk_master_dictionary(ends_with_lerLar)

    @unittest.skip("skipped")
    def test_should_remove_words_with_enAn_from_zemberek_master_dictionary(self):
        self._process_zemberek_master_dictionary(ends_with_enAn)

    @unittest.skip("skipped")
    def test_should_remove_words_with_enAn_from_trnltk_master_dictionary(self):
        self._process_trnltk_master_dictionary(ends_with_enAn)

    @unittest.skip("skipped")
    def test_should_remove_words_with_lanmakLenmek_from_zemberek_master_dictionary(self):
        self._process_zemberek_master_dictionary(ends_with_lanmakLenmek)

    @unittest.skip("skipped")
    def test_should_remove_words_with_lanmakLenmek_from_trnltk_master_dictionary(self):
        self._process_trnltk_master_dictionary(ends_with_lanmakLenmek)

    @unittest.skip("skipped")
    def test_should_remove_words_with_siSu_from_zemberek_master_dictionary(self):
        self._process_zemberek_master_dictionary(ends_with_siSu)

    @unittest.skip("skipped")
    def test_should_remove_words_with_siSu_from_trnltk_master_dictionary(self):
        self._process_trnltk_master_dictionary(ends_with_siSu)

    @unittest.skip("skipped")
    def test_should_remove_words_with_likLuk_from_zemberek_master_dictionary(self):
        self._process_zemberek_master_dictionary(ends_with_likLuk)

    @unittest.skip("skipped")
    def test_should_remove_words_with_likLuk_from_trnltk_master_dictionary(self):
        self._process_trnltk_master_dictionary(ends_with_likLuk)

    @unittest.skip("skipped")
    def test_should_remove_words_with_caCe_from_zemberek_master_dictionary(self):
        self._process_zemberek_master_dictionary(ends_with_caCe)

    @unittest.skip("skipped")
    def test_should_remove_words_with_caCe_from_trnltk_master_dictionary(self):
        self._process_trnltk_master_dictionary(ends_with_caCe)

    @unittest.skip("skipped")
    def test_should_remove_words_with_casinaCesine_from_zemberek_master_dictionary(self):
        self._process_zemberek_master_dictionary(ends_with_casinaCesine)

    @unittest.skip("skipped")
    def test_should_remove_words_with_casinaCesine_from_trnltk_master_dictionary(self):
        self._process_trnltk_master_dictionary(ends_with_casinaCesine)

    @unittest.skip("skipped")
    def test_should_remove_words_with_dirmakDirmek_from_zemberek_master_dictionary(self):
        self._process_zemberek_master_dictionary(ends_with_dirmakDirmek)

    @unittest.skip("skipped")
    def test_should_remove_words_with_dirmakDirmek_from_trnltk_master_dictionary(self):
        self._process_trnltk_master_dictionary(ends_with_dirmakDirmek)

    @unittest.skip("skipped")
    def test_should_remove_words_with_tmakTmek_from_zemberek_master_dictionary(self):
        self._process_zemberek_master_dictionary(ends_with_tmakTmek)

    @unittest.skip("skipped")
    def test_should_remove_words_with_tmakTmek_from_trnltk_master_dictionary(self):
        self._process_trnltk_master_dictionary(ends_with_tmakTmek)

    @unittest.skip("skipped")
    def test_should_remove_words_with_rmakRmek_from_zemberek_master_dictionary(self):
        self._process_zemberek_master_dictionary(ends_with_rmakRmek)

    @unittest.skip("skipped")
    def test_should_remove_words_with_rmakRmek_from_trnltk_master_dictionary(self):
        self._process_trnltk_master_dictionary(ends_with_rmakRmek)

    @unittest.skip("skipped")
    def test_should_remove_words_with_ilmakIlmek_from_zemberek_master_dictionary(self):
        self._process_zemberek_master_dictionary(ends_with_ilmakIlmek)

    @unittest.skip("skipped")
    def test_should_remove_words_with_ilmakIlmek_from_trnltk_master_dictionary(self):
        self._process_trnltk_master_dictionary(ends_with_ilmakIlmek)

    @unittest.skip("skipped")
    def test_should_remove_words_with_mazMez_from_zemberek_master_dictionary(self):
        self._process_zemberek_master_dictionary(ends_with_mazMez)

    @unittest.skip("skipped")
    def test_should_remove_words_with_mazMez_from_trnltk_master_dictionary(self):
        self._process_trnltk_master_dictionary(ends_with_mazMez)

    @unittest.skip("skipped")
    def test_should_remove_words_with_misMus_from_zemberek_master_dictionary(self):
        self._process_zemberek_master_dictionary(ends_with_misMus)

    @unittest.skip("skipped")
    def test_should_remove_words_with_misMus_from_trnltk_master_dictionary(self):
        self._process_trnltk_master_dictionary(ends_with_misMus)

    @unittest.skip("skipped")
    def test_should_remove_words_with_ciCu_from_zemberek_master_dictionary(self):
        self._process_zemberek_master_dictionary(ends_with_ciCu)

    @unittest.skip("skipped")
    def test_should_remove_words_with_ciCu_from_trnltk_master_dictionary(self):
        self._process_trnltk_master_dictionary(ends_with_ciCu)

    def _process_zemberek_master_dictionary(self, function):
        with codecs.open(self._get_zemberek_resource("master-dictionary.dict"), encoding="utf-8") as dict_file:
            with codecs.open(self._get_local_resource("new-master-dictionary.dict"), mode="w",
                encoding="utf-8") as new_dict_file:
                for line in dict_file:
                    if function(line.strip()):
                        print line.strip()
                    else:
                        new_dict_file.write(line)

    def _process_trnltk_master_dictionary(self, function):
        with codecs.open(self._get_trnltk_resource("master-dictionary.dict"), encoding="utf-8") as dict_file:
            with codecs.open(self._get_local_resource("new-master-dictionary.dict"), mode="w",
                encoding="utf-8") as new_dict_file:
                for line in dict_file:
                    if function(line.strip()):
                        print line.strip()
                    else:
                        new_dict_file.write(line)

    def _get_local_resource(self, str):
        return os.path.join(os.path.dirname(__file__), str)

    def _get_trnltk_resource(self, str):
        return os.path.join(os.path.dirname(__file__), Trnltk_Resources_Folder + str)

    def _get_zemberek_resource(self, str):
        return os.path.join(os.path.dirname(__file__), Zemberek_Resources_Folder + str)